package com.fanfanwang.ffw.app.crash;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ErrorManager {


	private SQLiteDatabase db;

	public ErrorManager(){
//		SqliteDb dbHelper = SqliteDb.getInstance();
//		db = dbHelper.openDatabaseConnect(SqliteDb._DATABASE_INFO_NAME);
	}
	public ArrayList<HashMap<String,Object>> querySql(){
		ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
		
		try {
			Cursor cursor = db.query("errorInfo", null, null, null, null, null, null);
			if(cursor.getCount()>0){
				cursor.moveToFirst();
				while(cursor.moveToNext()){
					HashMap<String, Object> errorMap = new HashMap<String, Object>();
					errorMap.put(ErrorInfo.ROW_ERROR_TITLE, cursor.getString(ErrorInfo.ROWID_ERROR_TITLE));
					errorMap.put(ErrorInfo.ROW_ERROR_CONTENT, cursor.getString(ErrorInfo.ROWID_ERROR_CONTENT));
					errorMap.put(ErrorInfo.ROW_ERROR_TIME, cursor.getString(ErrorInfo.ROWID_ERROR_TIME));
					errorMap.put(ErrorInfo.ROW_ERROR_ISUPLOAD, cursor.getString(ErrorInfo.ROWID_ERROR_ISUPLOAD));
					list.add(errorMap);
				}
			}
//		if(Integer.parseInt(Build.VERSION.SDK) < 14){
				
				cursor.close();
				cursor = null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		}
		
		return list;
	}

	public long insert(ErrorInfo info) {
		long insertId = 0;
		try {
			ContentValues values = new ContentValues(); 
			values.put(ErrorInfo.ROW_ERROR_TITLE, info.getErrorTitle());
			String content = info.getErrorContent();
			content = content.replaceAll("\\n", "[n]");
			content = content.replaceAll("\\r", "[r]");
			values.put(ErrorInfo.ROW_ERROR_CONTENT, content);
			insertId = db.insert("errorInfo", null, values);
			values.clear();
			values = null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return insertId;
	
	}

	public void close() {
//		SqliteDb.closeSqliteDatabase(db);
	}

}
